package server;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import requesthandler.SessionImpl;
public class SysSpyServer {
	ServerSocket serverSocket ;
	Socket clientSocket;
	boolean alive = true;
	
	public SysSpyServer(){
		try{
			serverSocket = new ServerSocket(18881);
			//System.out.println("started");
		}catch(IOException ex){
		    //do nothings
		}
	}
	private void startSession(Socket clientSocket){
		Thread client = new Thread(new SessionImpl(clientSocket, this));
		client.start();
	}
	public void start(){
		Socket clientSocket = null;
		if(serverSocket!=null){
			while(alive){
				try {
					//System.out.println("listening ...");
				    clientSocket = serverSocket.accept();
				    startSession(clientSocket);
				} catch (IOException ex) {
					//do nothing
				}
			}
			//System.out.println("Server Stopped");
			
			clientSocket = null;
			try{
				serverSocket.close();
				serverSocket = null;
			}catch(Exception ex){
				
			}
		}		
	}
	
	public void stop(){
		alive = false;
	}
}
